﻿using System;
using System.Collections.Generic;
using System.Transactions;
using Bingosoft.Data;
using HHMMS.WorkFlow.Model;

namespace HHMMS.WorkFlow.Service
{
	/// <summary>
	///表示对表WorkflowActivity的所有操作的实现。
	/// </summary>
    public class WorkflowActivityService
    {
        private readonly Dao dao = Dao.Get();

        private WorkflowActivityService()
        { }

        private static WorkflowActivityService instance = null;
        /// <summary>
        /// WorkflowActivity服务类的单例。
        /// </summary>
        public static WorkflowActivityService Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (typeof(WorkflowActivityService))
                    {
                        instance = new WorkflowActivityService();
                    }
                }
                return instance;
            }
        }

        public void Delete(int workflowId, int activityId)
        {
            this.dao.ExecuteNonQuery("workflow.workflowactivity.delete", new
            {
                ActivityId = activityId,
                WorkflowId = workflowId
            });
        }

        public void Insert(WorkflowActivity workflowActivity)
        {
            this.dao.ExecuteNonQuery("workflow.workflowactivity.insert", workflowActivity);
        }

        public void Update(WorkflowActivity workflowActivity)
        {
            this.dao.ExecuteNonQuery("workflow.workflowactivity.update", workflowActivity);
        }

        public WorkflowActivity Single(int workflowId, int activityId)
        {
            return this.dao.QueryEntity<WorkflowActivity>("workflow.workflowactivity.single", new
            {
                ActivityId = activityId,
                WorkflowId = workflowId
            });
        }

        /// <summary>
        /// 获取开始环节。
        /// </summary>
        /// <param name="workflowId"></param>
        /// <returns></returns>
        public WorkflowActivity GetStartActivity(int workflowId)
        {
            return this.dao.QueryEntity<WorkflowActivity>("workflow.workflowactivity.single", new
            {
                WorkflowId = workflowId,
                IsStart = true
            });
        }

        /// <summary>
        /// 获取结束环节。
        /// </summary>
        /// <param name="workflowId"></param>
        /// <returns></returns>
        public WorkflowActivity GetEndActivity(int workflowId)
        {
            return this.dao.QueryEntity<WorkflowActivity>("workflow.workflowactivity.single", new
            {
                WorkflowId = workflowId,
                IsEnd = true
            });
        }
    }
}